package com.api.interf;

import com.api.util.*;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 测试基类
 */
public class BaseProcessor1 {
    String[] cellNames={"ApiId","CaseId","Params","PreValidateSql","AfterValidateSql"};
    @Test(dataProvider = "datas")
    public void test(String apiId,String caseId,String params,String preValidateSql,String afterValidateSql){
        //System.out.println("caseid:"+caseId+",parase:"+params);
        //替换变量：
        params= VariableConfigurationUtil.replaceParam(params);

        preValidateSql=VariableConfigurationUtil.replaceParam(preValidateSql);
        afterValidateSql=VariableConfigurationUtil.replaceParam(afterValidateSql);
        if(preValidateSql!=null&&preValidateSql.trim().length()>0){
            //接口调用前的验证
            String preValidateResult = DBCheckUtil.validate(preValidateSql);
            //保存要写入得数据
            ExcelUtil.saveWriteBackData("用例",caseId,"PreValidateResult",preValidateResult);
        }

        //根据接口编号获取接口地址
        String url = RestUtil.getUrlByApiId(apiId);
        //根据接口编号获取借口类型
        String type = RestUtil.getTypeByApiId(apiId);
        //完成接口调用，拿到接口响应报文字符串
        String result = RestUtil.process(url,type,params);

        //ActualResponseData(实际响应数据)
        //CaseId>行号,
        //使用集合对象保存数据，避免多次读数据
        ExcelUtil.saveWriteBackData("用例",caseId,"ActualResponseData",result);
        //批量执行写入数据
        //ExcelUtil.writeData(PropertiesUtil.getCaseFilePath(),"用例",caseId,"ActualResponseData",result);
        if(afterValidateSql!=null&&afterValidateSql.trim().length()>0){
            //接口调用后的验证
            String afterValidateResult = DBCheckUtil.validate(afterValidateSql);
            //保存要写入得数据
            ExcelUtil.saveWriteBackData("用例",caseId,"AfterValidateResult",afterValidateResult);
        }

    }
    @AfterSuite
    public void tearDown(){
        //从配置文件中读取路径

        //批量回写测试数据

        ExcelUtil.batchWriteBackDatas(PropertiesUtil.getCaseFilePath());

    }
}
